package com.zhangyu.shadow.ec.laucher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.tepth.shadow.ui.launcher.ILauncherListener;
import com.tepth.shadow.ui.launcher.LauncherScrollTag;
import com.tepth.shadow.ui.launcher.OnLauncherFinishTag;
import com.zhangyu.shadow.core.account.AccountManager;
import com.zhangyu.shadow.core.account.IUserChecker;
import com.zhangyu.shadow.core.delegate.ShadowDelegate;
import com.zhangyu.shadow.core.util.storage.ShadowPreference;
import com.zhangyu.shadow.core.util.timer.ITimerListener;
import com.zhangyu.shadow.core.util.timer.TimerUtil;
import com.zhangyu.shadow.ec.R;
import com.zhangyu.shadow.ec.R2;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

/**
 * @author zhangyu
 * @date 2017/10/20
 */

public class LauncherDelegate extends ShadowDelegate {

    @BindView(R2.id.tv_timer)
    TextView mTvTimer;

    private static final int MAX_TIME = 5;
    private TimerUtil mTimerUtil;

    private ILauncherListener mILauncherListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if(activity instanceof ILauncherListener){
            mILauncherListener = (ILauncherListener) activity;
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_laucher;
    }

    @OnClick(R2.id.tv_timer)
    void onTimerClick() {
        mTimerUtil.cancel();
        checkIsFirstLauncher();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initTimer();
    }

    private void initTimer() {
        mTimerUtil = new TimerUtil();
        mTimerUtil.startTimer(MAX_TIME, new ITimerListener() {

            @Override
            public void onTimer(@NonNull Long along) {
                if (mTvTimer != null) {
                    mTvTimer.setText(MessageFormat.format("跳过\n{0}s", along));
                }
            }

            @Override
            public void onComplete() {
                checkIsFirstLauncher();
            }
        });
    }

    private void checkIsFirstLauncher() {
        if (!ShadowPreference.getAppFlag(LauncherScrollTag.HAS_FIRST_LAUNCHER_TAG.name())) {
            startWithPop(new LauncherScrollDelegate());
        } else {
            //检查用户是否已经登录
            AccountManager.checkAccount(new IUserChecker() {
                @Override
                public void onSignIn() {
                    if(mILauncherListener != null){
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.SIGNED);
                    }
                }

                @Override
                public void onNotSignIn() {
                    if(mILauncherListener != null){
                        mILauncherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
                    }
                }
            });
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mTimerUtil.cancel();
    }
}
